#!/bin/bash
## 启动Docker daemon

blue()  { echo -e "\033[34m$1\033[0m"; }
red()   { echo -e "\033[31m$1\033[0m"; }

# 检查Docker daemon是否运行
check_docker() {
    if ! docker info > /dev/null 2>&1; then
        red "Docker daemon 未运行"
        return 1
    else
        echo "Docker daemon 已运行"
        return 0
    fi
}

# 启动Docker daemon (针对macOS)
start_docker() {
    echo "尝试启动Docker daemon..."
    open -a Docker
    # 等待Docker启动
    for i in {1..30}; do
        if docker info > /dev/null 2>&1; then
            echo "Docker daemon 启动成功"
            return 0
        fi
        echo "等待Docker启动... ($i/30)"
        sleep 2
    done
    echo "Docker daemon 启动超时"
    return 1
}

# 检查并启动Docker
check_docker
if [ $? -ne 0 ]; then
    start_docker
    if [ $? -ne 0 ]; then
        red "无法启动Docker，退出脚本"
        exit 1
    fi
fi
